
--spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
-- Setup spi with `clock_div` of 16 and spi.FULLDUPLEX
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 16,spi.FULLDUPLEX)

gpio.mode(1,gpio.OUTPUT)
gpio.write(1,gpio.HIGH)

cs  = 8 -- GPIO15, pull-down 10k to GND
dc  = 4 -- GPIO2
res = 0 -- GPIO16, RES is optional YMMV

-- we won't be using the HSPI /CS line, so disable it again
gpio.mode(cs, gpio.INPUT, gpio.PULLUP)

disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)

disp:begin(ucg.FONT_MODE_TRANSPARENT)
disp:setFont(ucg.font_ncenR14_hr)
disp:clearScreen()

--disp:setFont(ucg.font_7x13B_tr)
disp:setColor(0, 0, 255);
disp:drawBox(0, 0, 240, 320);




-- SETTING UP TOUCH
cs_pin  = 2 -- GPIO4
irq_pin = 3 -- GPIO0
height  = 240
width = 320
xpt2046.init(cs_pin, irq_pin, width, height)
--xpt2046.setCalibration(198, 1776, 1762, 273)
xpt2046.setCalibration(1920, 0, 128, 1984)
gpio.mode(irq_pin,gpio.INT,gpio.PULLUP)

local X,Y
X=0
Y=0

gpio.trig(irq_pin, "down", function()
    x,y=xpt2046.getPositionAvg()
    if(x~=-1 and y~=-1)then
        disp:setColor(0, 0, 255);
        disp:drawBox(X, Y-20, 120, 20);
        disp:setColor(0, 255, 255);
        disp:setPrintPos(x,y);
        disp:print("YES I DO!");
        X,Y=x,y
    end
end)
